<%@ page language="java"  contentType="text/html;charset=UTF-8"%>
<%@ include file="/commons/taglibs.jsp"%>
<html>
<head>
<title>资源管理</title>
<%@ include file="/commons/meta.jsp" %>
<%@ include file="/commons/header.jsp" %>
<%@ include file="/extjs/jsps/importExt.jsp" %>
<%@ include file="/extjs/jsps/importEasyExt.jsp" %>
<script language="javascript" type="text/javascript">
var checkedNode;

var treePanel = new Ext.tree.TreePanel({
	title: '资源管理',
	style: 'text-align:left;',
	autoScroll: true,
	frame: false,
	enableDD: true,
	containerScroll: true,
	rootVisible: true,
	border: false,
	animate: true,
    root: new Ext.tree.AsyncTreeNode({
		id: '1',
		text: '${ROOT_NAME}',
		leaf: false,
		expanded: true
    }),
	loader: new Ext.tree.TreeLoader({
		url: '${ctx}/resource!listJSON.action'
	})
});

function appendChild(obj) {
	checkedNode.leaf=false;
	var newNode=new Ext.tree.AsyncTreeNode({
		id: obj.id,
		text: obj.name,
		leaf: obj.isLeaf,
		orderNum: obj.orderNum
	});
	checkedNode.appendChild(newNode);
}

function setTreeNode(obj) {
	if(obj.name) {
		checkedNode.setText(obj.name);
	}
	if(checkedNode.isLeaf()!=obj.isLeaf) {
		var newNode=new Ext.tree.AsyncTreeNode({
	   		id: checkedNode.id,
			text: checkedNode.text,
			leaf: obj.isLeaf,
			orderNum: checkedNode.attributes.orderNum
	   	});
	   	checkedNode.parentNode.replaceChild(newNode,checkedNode);
	   	newNode.select();
	   	checkedNode=newNode;
	}
}

Ext.onReady(function() {

	// 定义右键菜单 
	var rightClick = new Ext.menu.Menu({ 
		items: [{
			text: '新增资源',
			iconCls: 'addBtn',
			handler: function() {
				var paraUrl='&parentId='+checkedNode.id;
				var url='${ctx}/resource!input.action?winId=winInsertResource&U='+new Date().getTime()+paraUrl;
				windowOpen(url,'winInsertResource','新建资源',285,755,'','',{
					onClose: function(scope) {
						var obj=scope.returnValue;
						if(obj) {
							appendChild(obj);
						}
					}
				});
			}
		},{
			text: '编辑资源',
			iconCls: 'editBtn',
			handler: function() {
				var paraUrl='&id='+checkedNode.id;
				var url='${ctx}/resource!input.action?winId=winUpdateResource&U='+new Date().getTime()+paraUrl;
				windowOpen(url,'winUpdateResource','更新资源',285,755,'','',{
					onClose: function(scope) {
						var obj=scope.returnValue;
						if(obj) {
							setTreeNode(obj);
						}
					}
				});
			}
		},{
			text: '删除资源',
			iconCls: 'deleteBtn',
			handler: function() {
				Ext.Msg.confirm('',(checkedNode.isLeaf()?'':'会同时删除下级资源，')+'确认删除？',function(e) {
					if(e!='yes') return;
					Ext.Ajax.request({
						url: '${ctx}/resource!deleteJSON.action',
						params: {
							id: checkedNode.id
						},
						success: function(response,opts) {
							var result = Ext.decode(response.responseText);
							Common.parse(result,function(buttonId) {
								Ext.Msg.hide();
								if(result.pass||buttonId=='yes') {
									checkedNode.remove();
								}
							});
						},
						failure: function(response,opts) {
							Ext.Msg.hide();
		        			Common.parseFailureResponse(response);
						}
					});
				});
			}
		}]
	});
	
	// 增加右键点击事件 
	treePanel.on('contextmenu',function(node, event) {
		event.preventDefault();// 阻止浏览器默认右键菜单显示
		
		if(node.id=='1') {
			rightClick.get(0).show();// 新增资源
			rightClick.get(1).show();// 编辑资源
			rightClick.get(2).hide();// 删除资源
		}else if(node.isLeaf()) {
			rightClick.get(0).hide();
			rightClick.get(1).show();
			rightClick.get(2).show();
		}else {
			rightClick.get(0).show();
			rightClick.get(1).show();
			rightClick.get(2).show();
		}
		node.select();
		checkedNode=node;
		rightClick.showAt(event.getXY());// 取得鼠标点击坐标，展示菜单 
	}); 
	
	treePanel.on('nodedrop',function(e) { 
		var obj=new Object();
		obj.id=e.dropNode.id;
		obj.parentId=e.target.parentNode.id;
		if(e.point=='append') {
			obj.parentId=e.target.id;
		}else if(e.point=='above') {
			obj.orderNum=e.target.attributes.orderNum;
		}else if(e.point=='below') {
			if(e.dropNode.attributes.orderNum>e.target.attributes.orderNum) {
				obj.orderNum=e.target.attributes.orderNum+1;
			}else {
				obj.orderNum=e.target.attributes.orderNum;
			}
		}
		Ext.Ajax.request({
			url: '${ctx}/resource!sortJSON.action',
			params: obj,
			success: function(response,opts) {
				var result=Ext.decode(response.responseText);
				Common.parse(result,function(buttonId) {
					Ext.Msg.hide();
					if(result.pass||buttonId=='yes') {
					}
				});
			},
			failure: function(response,opts) {
				Ext.Msg.hide();
		        Common.parseFailureResponse(response);
			}
		});
	}); 
	
	var viewport = new Ext.Viewport({
		layout: 'fit',
		items: [treePanel]
	});
	
});
</script>
</head>

<body>
</body>
</html>
